package builtin

import (
	"talent.com/agency_im/common/msg"
	"talent.com/agency_im/common/utils/frequency"
	"talent.com/agency_im/modules/core"
	"talent.com/server/durotar.git/log"
)

type FrequencyLimiterInterceptor struct {
	Limiter frequency.Limiter
}

func (f *FrequencyLimiterInterceptor) PreHandle(message *msg.Message) (bool, error) {
	log.Debug("pre handle message on frequency limiter, message: %+v", message)
	if message.SendSelf { // 只对用户触发的消息限流，系统自动发送消息通过
		if pass, err := f.Limiter.Check("SEND_MSG", message.SourceID); err != nil {
			return false, err
		} else {
			if !pass {
				log.Debug("user send message so frequently, user id: [%v]", message.SourceID)
				core.SendErrorMsg(message.SourceID, "消息发送太频繁")
			}
			return pass, nil
		}
	}
	return true, nil
}

func (f *FrequencyLimiterInterceptor) PostHandle(message *msg.Message) error {
	return nil
}
